﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditFeeFrm.aspx.cs" Inherits="WebApp.EditFeeFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>费用资料</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="js/Jquery.Query.js"></script>
    <script type="text/javascript" src="js/EasyUIExt.js"></script>

    <script type="text/javascript">
        $(window).load(function() {
            $('#eFeeType').combobox('disable');
            initParams();
            defaultInit();
        });

        function defaultInit() {
            $('#eFeeType').combobox('setValue', $('#feeType').val());
            $('#eOpTime').datebox('setValue', DateFormatter(new Date()));
            $('#eOperator').combogrid('setValue', <%=(Session["User"] as WebApp.BaseDataSvc.TB_User).id%>);
            var feeTarget = $.query.get('FeeTarget');
            if(feeTarget)
                $('#eTarget').combobox('setValue', feeTarget);     
        }

        function initParams() {
            var orderId = $.query.get('OrderId');
            var feeType = $.query.get('FeeType');
            $('#feeType').val(feeType);
            var feeId = $.query.get('FeeId');
            $('#orderId').val(orderId);
            $('#editId').val(feeId);
            initData(feeType);
        }

        function bindFee() {
            var id = $('#editId').val();
            if (id) {
                WebApp.EditFeeFrm.GetFee(id, function callback(p) {
                    if (p.error) {
                        $.messager.alert('异常消息', '获取数据异常,' + p.error.Message, 'error');
                        return;
                    }

                    var res = p.value;
                    if (res) {
                        var data = res.Data;
                        var msg = res.Msg;
                        if (msg)
                            $.messager.alert('费用信息获取异常', msg, 'error');
                        else {
                            if (data) {
                                $('#eFeeType').combobox('setValue', data.feeType);
                                $('#eCode').val(data.code);
                                $('#eItem').combobox('setValue', data.item);
                                $('#eTarget').combobox('setValue', data.target);
                                $('#eAmount').numberbox('setValue', data.amount);
                                $('#eUnit').combobox('setValue', data.unit);
                                $('#eOperator').combogrid('setValue', data.operator);
                                $('#eOpTime').datebox('setValue', DateFormatter(data.opTime));
                                $('#eMemo').val(data.memo);
                            }
                        }
                    }
                });
            }
        }



        function setTargetField(o) {
            return o;
        }

        function bindTarget(params, v, t) {
            var u = 'JSONHandler.ashx?' + params;
            $(function() {
                $('#eTarget').combobox({
                    valueField: setTargetField(v),
                    textField: setTargetField(t)
                });
            });

            $.getJSON(u, function(result) {
                $('#eTarget').combobox('loadData', result);
                bindFee();
            });
        }

        function initData(feeType) {

            $('#eOpTime').datebox('setValue', DateFormatter(new Date()));

            $.getJSON("JSONHandler.ashx?DataType=FeeType", function(result) {
                $('#eFeeType').combobox('loadData', result);
                $('#eFeeType').combobox('setValue', feeType);
            });

            $.getJSON("JSONHandler.ashx?DataType=FeeItems", function(result) {
                $('#eItem').combobox('loadData', result);
            });

            $.getJSON("JSONHandler.ashx?DataType=User", function(result) {
                $('#eOperator').combogrid("grid").datagrid("loadData", result);
            });

            if (feeType == 1)
                bindTarget('DataType=Client&AddKey=false', 'id', 'fullName');
            else
                bindTarget('DataType=FeeTarget', 'id', 'name');

            $.getJSON("JSONHandler.ashx?DataType=Unit", function(result) {
                $('#eUnit').combobox('loadData', result);
                $('#eUnit').combobox('selectedIndex',0);
            });

        }

        function resetFrm() {
            $('#fm').form('clear');
            $('#editId').val('');
            defaultInit();
            $('#eUnit').combobox('selectedIndex',0);
        }

        function addFee() {
            resetFrm();
        }

        function saveFee() {
            var validate = $('#fm').form('validate');
            if (!validate)
                return;
            var id = $('#editId').val();
            var orderId = $('#orderId').val();
            var eFeeType = $('#eFeeType').combobox('getValue');
            var eCode = $('#eCode').val();
            var eItem = $('#eItem').combobox('getValue');
            var eTarget = $('#eTarget').combobox('getValue');
            var eAmount = $('#eAmount').numberbox('getValue');
            var eUnit = $('#eUnit').combobox('getValue');
            var eOperator = $('#eOperator').combogrid('getValue');
            var eOpTime = $('#eOpTime').datebox('getValue');
            var eMemo = $('#eMemo').val();

            WebApp.EditFeeFrm.SaveFee(id, orderId, eFeeType, eItem, eCode, eAmount, eMemo, eOperator, eOpTime, eUnit, eTarget, function callback(p) {

                if (p.error) {
                    $.messager.alert('异常消息', '提交保存异常,' + p.error.Message, 'error');
                    return;
                }

                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('费用信息获取异常', msg, 'error');
                    else {
                        //refresh();
                        refreshParent(orderId, eFeeType)
                        if (id) {
                            if (data > 0) {
                                $.messager.alert('消息', '费用信息已更新!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('费用信息更新异常', msg, 'error');
                        }
                        else {
                            if (data > 0) {
                                $.messager.alert('消息', '费用信息已创建!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('费用信息创建异常', msg, 'error');
                        }
                    }
                }
            });
        }

        function deleteFee() {
            $.messager.confirm('提示', '确认删除该项吗?', function(r) {
                if (r) {
                    var id = $('#editId').val();
                    var orderId = $('#orderId').val();
                    var feeType = $('#feeType').val();
                    WebApp.EditFeeFrm.DeleteFee(id, orderId, function callback(p) {

                        if (p.error) {
                            $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                            return;
                        }
                        var res = p.value;
                        if (res) {
                            var data = res.Data;
                            var msg = res.Msg;
                            if (msg)
                                $.messager.alert('删除异常', msg, 'error');
                            else {
                                if (data == true) {
                                    $.messager.alert('消息', '费用信息已删除!', 'info');
                                    refreshParent(orderId, feeType);
                                }
                                else
                                    $.messager.alert('删除费用失败', msg, 'error');
                            }
                        }
                    });
                }
            });
        }

        function varifyFee(pass) {
            var id = $('#editId').val();
            var feeType = $('#feeType').val();

            WebApp.EditFeeFrm.VerifyFee(pass, id, function callback(p) {

                if (p.error) {
                    $.messager.alert('异常消息', '提交审核异常,' + p.error.Message, 'error');
                    return;
                }
                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('审核异常', msg, 'error');
                    else {
                        if (data == true) {
                            if (pass)
                                $.messager.alert('消息', '费用信息已批准!', 'info');
                            else
                                $.messager.alert('消息', '费用信息已反审批!', 'info');
                            refreshParent(orderId, feeType);
                        }
                        else
                            $.messager.alert('审核费用失败', msg, 'error');
                    }
                }
            });
        }

        function refreshParent(orderId, feeType) {
            window.parent.bindFee(orderId, feeType);
            //window.parent.JqueryDialog.SubmitCompleted("执行回调函数JqueryDialog.SubmitCompleted，关闭Dialog！并刷新页面！", true, true);
        }
    </script>

</head>
<body>
    <form id="Form1" method="post" runat="server">
    </form>
    <div>
        <form id="fm">
        <div style="padding: 5px; border: 1px solid #ddd">
            <a href="#" id="btnAdd" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'"
                onclick="addFee()">新增</a> <a href="#" id="btnSave" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'"
                    onclick="saveFee()">保存</a> <a href="#" id="btnDelete" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'"
                        onclick="deleteFee()">删除</a> <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-ok'">
                            审核</a>
        </div>
        <div id="mm1" style="width: 150px;">
            <div data-options="iconCls:'icon-ok'" onclick="varifyFee(true)">
                批准</div>
            <div data-options="iconCls:'icon-undo'" onclick="varifyFee(false)">
                反审批</div>
        </div>
        <div class="easyui-panel" title="费用资料" style="width: 500px; padding: 5px;">
            <table style="width: 100%; padding: 10px">
                <tr>
                    <td style="text-align: left">
                        费用类型：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eFeeType" data-options="valueField:'Value',textField:'Name'"
                            style="width: 150px;" required="true">
                    </td>
                    <td style="text-align: left">
                        账单号：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eCode" style="width: 150px" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        费目名称：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eItem" data-options="valueField:'id',textField:'name'"
                            style="width: 150px;" required="true">
                    </td>
                    <td style="text-align: left">
                        结算对象：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eTarget" data-options="valueField:'id',textField:'name'"
                            style="width: 150px;" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        金额：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eAmount" style="width: 150px" class="easyui-numberbox" data-options="min:0,precision:2"
                            required="true" />
                    </td>
                    <td style="text-align: left">
                        单位：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eUnit" data-options="valueField:'id',textField:'name'"
                            style="width: 150px;" required="true"">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        制单人：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combogrid" required="true" id="eOperator" style="width: 150px"
                            data-options="  
            panelWidth: 150,  
            idField: 'id',  
            textField: 'userName',
            columns: [[{field:'userName',title:'名称',width:150}               
            ]],  
            fitColumns: true  
        ">
                    </td>
                    <td style="text-align: left">
                        制单日期：
                    </td>
                    <td style="text-align: left">
                        <input id="eOpTime" class="easyui-datebox" data-options="formatter:DateFormatter,parser:DateParser"
                            style="width: 100px" required="true" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        备注：
                    </td>
                    <td style="text-align: left" colspan="3">
                        <input type="text" id="eMemo" style="width: 150px" />
                    </td>
                </tr>
            </table>
        </div>
        <input id="editId" type="hidden" />
        </form>
        <input id="orderId" type="hidden" />
        <input id="feeType" type="hidden" />
    </div>
</body>
</html>
